﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PanGu;
using PanGu.Dict;
using System.Web;

namespace WordSegments
{
    public class OPSegmentUtils
    {
        private PanGu.Match.MatchOptions _options;
        private PanGu.Match.MatchParameter _parameters;

        static OPSegmentUtils instance;

        public static OPSegmentUtils sharedInstance
        {
            get
            {
                if(instance == null)
                {
                    instance = new OPSegmentUtils();
                }
                return instance;
            }
        }
        public void Init(string dictPath)
        {
            Segment.Init(dictPath);
            _options = PanGu.Setting.PanGuSettings.Config.MatchOptions;
            _parameters = PanGu.Setting.PanGuSettings.Config.Parameters;

            _options.FrequencyFirst = true;
            _options.FilterStopWords = true;
            _options.ChineseNameIdentify = true;
            _options.MultiDimensionality = true;
            _options.EnglishMultiDimensionality = false;
            _options.ForceSingleWord = false;
            _options.TraditionalChineseEnabled = false;
            _options.OutputSimplifiedTraditional = false;
            _options.UnknownWordIdentify = true;
            _options.FilterEnglish = false;
            _options.FilterNumeric = true;
            _options.IgnoreCapital = true;
            _options.EnglishSegment = false;
            _options.SynonymOutput = false;
            _options.WildcardOutput = false;
            _options.WildcardSegment = false;
            _options.CustomRule = false;

            _parameters.Redundancy = 0;
            _parameters.FilterEnglishLength = 0;
            _parameters.FilterNumericLength = 0;
        }

        public ICollection<WordInfo> doSegments(string stringToSeg)
        {
            Segment segment = new Segment();
            return segment.DoSegment(stringToSeg, _options, _parameters);
        }

        public string strFenCiIndexer(string stringToIndex)
        {
            ICollection<WordInfo> words = doSegments(stringToIndex);
            string indexer = string.Join(" ", words.Select(w => w.Word).Distinct());
            return indexer;
        }
    }
}